package com.yunhai.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.model.entity.ums.UmsPermission;
import com.yunhai.ucenter.mapper.SysMenuMapper;
import com.yunhai.ucenter.service.SysMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author mengze
 * @since 2023-08-23
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public void addMenu(UmsPermission sysMenu) throws Exception {
        sysMenuMapper.insert(sysMenu);
        return;
    }

    @Override
    public void updateMenu(UmsPermission sysMenu) throws Exception {
        sysMenuMapper.updateById(sysMenu);
        return;
    }

    @Override
    public void batchRemoveMenu(List<Long> ids) throws Exception {

        sysMenuMapper.deleteBatchIds(ids);
        return;
    }

    @Override
    public IPage paginationMenuList(PaginationVo paginationVo) throws Exception {

        IPage page = new Page(paginationVo.getPageIndex(), paginationVo.getPageSize());

        LambdaQueryWrapper<UmsPermission> lqs = new LambdaQueryWrapper();
        lqs.eq(UmsPermission::getParentId, 0);
        sysMenuMapper.selectPage(page, lqs);

        List<UmsPermission> sysMenus = page.getRecords();
        if( sysMenus == null || sysMenus.size() == 0 ){
            return page;
        }


        LambdaQueryWrapper<UmsPermission> hasParentLqs = new LambdaQueryWrapper();
        hasParentLqs.ne(UmsPermission::getParentId, 0);
        List<UmsPermission> hasParentPerissionList = sysMenuMapper.selectList( hasParentLqs );

        //2、然后组装成树形结构
        sysMenus.stream().filter(item->{
            //相当于拿第一级的菜单
            return item.getParentId() == 0;
        }).map( item->{
            List<UmsPermission> children = flatMenuList(item, hasParentPerissionList);
            item.setChildren(children);
            return  item;
        }).collect(Collectors.toList());

        return page;
    }

    @Override
    public List<UmsPermission> allMenuList() throws Exception {
        //1、先查出所有的权限菜单
        List<UmsPermission> sysMenus = sysMenuMapper.selectList(null);

        //2、然后组装成树形结构
        return sysMenus.stream().filter(item->{
            //相当于拿第一级的菜单
            return item.getParentId() == 0;
        }).map( item->{
            List<UmsPermission> children = flatMenuList(item, sysMenus);
            item.setChildren(children);
            return  item;
        }).collect(Collectors.toList());
    }


    private  List<UmsPermission> flatMenuList( UmsPermission parent, List<UmsPermission> list ){

        return list.stream().filter( item->{
            return item.getParentId().equals(parent.getId());
        }).map( item-> {
           List<UmsPermission> children =  flatMenuList( item, list );
           item.setChildren(children);
           return item;
        }).collect(Collectors.toList());
    }
}
